package org.tgit.gateway.config;

import java.util.Properties;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.tgit.gateway.common.ApiConstants;
import org.tgit.gateway.common.GatewayException;

import com.github.pagehelper.PageHelper;

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "org.tgit.gateway.mapper")
public class MybatisConfig implements TransactionManagementConfigurer {
	private final static Logger LOGGER = LoggerFactory.getLogger(MybatisConfig.class);
	@Autowired
	private DruidConfig druidConfig;

	@Bean
	@Override
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(druidConfig.mysqlDtaSource());
	}

	@Bean(name = "sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactoryBean() {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		try {
			sqlSessionFactoryBean.setDataSource(druidConfig.mysqlDtaSource());
		} catch (Exception e) {
			LOGGER.error("会话异常", e);
			throw new GatewayException(ApiConstants.DB_CONNECT_ERROR, "sqlSessionFactoryBeanException");
		}
		sqlSessionFactoryBean.setTypeAliasesPackage("org.tgit.gateway.domain");

		// 分页插件
		PageHelper pageHelper = new PageHelper();
		Properties properties = new Properties();
		properties.setProperty("reasonable", "true");
		properties.setProperty("supportMethodsArguments", "true");
		properties.setProperty("returnPageInfo", "check");
		properties.setProperty("params", "count=countSql");
		pageHelper.setProperties(properties);

		// 添加插件
		sqlSessionFactoryBean.setPlugins(new Interceptor[] { pageHelper });

		// 添加XML目录
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			sqlSessionFactoryBean.setMapperLocations(
					resolver.getResources("classpath*:/org/tgit/gateway/mapper/**/*Mapper.xml"));
			return sqlSessionFactoryBean.getObject();
		} catch (Exception e) {
			LOGGER.error("配置xml里sql语句为空或者映射对象为空", e);
			throw new GatewayException(ApiConstants.DB_CONNECT_ERROR, "sqlSessionFactoryBeanException");
		}
	}

	@Bean
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}